package com.zzw.controller;

import com.zzw.entity.LoginUser;
import com.zzw.entity.Result;
import com.zzw.entity.User;
import com.zzw.service.UserService;
import com.zzw.entity.CurrentUser;
import com.zzw.utils.DigestUtil;
import com.zzw.utils.TokenUtils;
import com.zzw.utils.WarehouseConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @Project: warehouse
 * @Description:
 * @Author: zzw
 */
@RestController
@Slf4j
@Api(tags = "登录模块")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 登录的url接口
     *
     * @param loginUser
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录操作")
    public Result login(@RequestBody LoginUser loginUser) {
        /*
        校验验证码
         */
        if (Boolean.FALSE.equals(stringRedisTemplate.hasKey(loginUser.getVerificationCode()))) {
            return Result.err(Result.CODE_ERR_BUSINESS, "验证码不正确！");
        }

        /*
        校验用户名和密码
         */
        User user = userService.findUserByCode(loginUser.getUserCode());
        if (user != null) {//查到了用户
            //查到的用户状态是已审核
            if (user.getUserState().equals(WarehouseConstants.USER_STATE_PASS)) {
                //将用户录入的密码进行加密
                String password = DigestUtil.hmacSign(loginUser.getUserPwd());
                if (password.equals(user.getUserPwd())) {//查到的用户的密码和用户录入的密码相同
                    //生成token并响应给前端
                    CurrentUser currentUser = new CurrentUser(user.getUserId(), user.getUserCode(), user.getUserName());
                    String token = tokenUtils.loginSign(currentUser, user.getUserPwd());
                    return Result.ok("登录成功！", token);
                } else {//查到的用户的密码和用户录入的密码不同
                    return Result.err(Result.CODE_ERR_BUSINESS, "密码不正确！");
                }
            } else {//查到的用户状态是未审核
                return Result.err(Result.CODE_ERR_BUSINESS, "用户未审核！");
            }
        } else {
            return Result.err(Result.CODE_ERR_BUSINESS, "该用户不存在！");
        }
    }

    /**
     * 获取当前登录用户信息的url接口
     * 将请求头Token的值即前端归还的token,赋值给请求处理方法的参数String clientToken
     *
     * @param clientToken
     * @return
     */
    @GetMapping("/curr-user")
    @ApiOperation("获取当前登录用户")
    public Result currUser(@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String clientToken) {
        //从前端归还的token中解析出当前登录用户的信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(clientToken);
        return Result.ok(currentUser);
    }

    /**
     * 登出接口的url接口
     * 将请求头Token的值即前端归还的token,赋值给请求处理方法的参数String clientToken
     *
     * @param clientToken
     * @return
     */
    @DeleteMapping("/logout")
    @ApiOperation("登出操作")
    public Result logout(@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String clientToken) {
        //从redis中移除token
        stringRedisTemplate.delete(clientToken);
        return Result.ok();
    }
}
